package uk.co.hs.web.initializer;

import java.util.Locale;
import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;

public abstract class HsConfigTemplate
{
  @Bean(name="hs_PSPC")
  public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
  {
    return new PropertySourcesPlaceholderConfigurer();
  }

  @Bean(name = "messageSource")
  public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource()
  {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("/WEB-INF/messages", "hs-messages");
    messageSource.setCacheSeconds(1);
    return messageSource;
  }

  @Bean
  public TilesConfigurer tilesConfigurer()
  {
    TilesConfigurer tilesConfigurer = new TilesConfigurer();
    String[] definitions = new String[1];
    definitions[0] = "/WEB-INF/tiles-defs.xml";
    tilesConfigurer.setDefinitions(definitions);
    return tilesConfigurer;
  }

  @Bean
  public UrlBasedViewResolver getTilesViewResolver()
  {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setViewClass(TilesView.class);
    resolver.setOrder(1);
    String[] viewNames = new String[2];
    viewNames[0] = "hs_*";
    viewNames[1] = "hs_UserList"; //TODO DAN - was in GAMMA, not sure if need this now ???
    resolver.setViewNames(viewNames);
    return resolver;
  }

  @Bean
  public InternalResourceViewResolver getJstlViewResolver()
  {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setOrder(2);
    resolver.setPrefix("/pages/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    return resolver;
  }

  @Bean
  public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
  {
    SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
    Properties exceptionMappings = new Properties();
    exceptionMappings.put("FrameworkException", "hs_Error");
    exceptionMappings.put("IllegalStateException", "hs_Error");
    exceptionMappings.put("Exception", "hs_Error");
    simpleMappingExceptionResolver.setExceptionMappings(exceptionMappings);
    return simpleMappingExceptionResolver;
  }

  @Bean
  public FixedLocaleResolver localeResolver()
  {
    FixedLocaleResolver fixedLocaleResolver = new FixedLocaleResolver();
    Locale locale = new Locale("en_GB");
    fixedLocaleResolver.setDefaultLocale(locale);
    return fixedLocaleResolver;
  }

  @Bean
  public MultipartResolver multipartResolver()
  {
    CommonsMultipartResolver cmpr = new CommonsMultipartResolver();
    cmpr.setMaxUploadSize(20971520);
    return cmpr;
  }
}
